< previous page page_246 next page >

Page 246
Although the manipulative statement tells the database engine what action to perform, the options declarations tell it what fields to process.
Note
You can't use SQL statements directly in a Visual Basic application. They're used with data controls or Data Access Objects.

Using the SELECT Statement
In most basic applications, the SQL statement type that you'll use most often is SELECT. The SELECT statement is used to retrieve records from the database and places them into a dynaset for access by the application code. The syntax of the SELECT statement is as follows:
SELECT [predicate] fieldlist FROM tablelist [table relations]
     [range options] [sort options] [group options]
Although you can create very complex SQL statements, as you can see from the syntax, the simplest form of the SELECT statement will retrieve all fields from a table. The following statement retrieves all fields and rows from the Contact table:
SELECT * FROM Contact
The asterisk (*) serves as a wildcard for the fieldlist section of the SELECT statement. The fieldlist defines the fields included in the output recordset. The fieldlist can include all fields in a table, only selected fields, or calculated values based on fields in the table. Also, by using the AS clause, you can rename a field to be used in the recordset.
Caution
Renaming fields doesn't affect the actual database field name, only the name used by the application to access that field in that particular recordset.

Because SQL allows you to retrieve data from multiple tables, you can even specify which table a field should be retrieved from. The syntax of the fieldlist section is as follows:
[tablename.]field1 [AS alt1][, [tablename.] field2 [AS alt2]]
By using this syntax, you can retrieve only the first and last name of a contact, as follows:
SELECT [First Name], [Last Name] from Contact

 
< previous page page_246 next page >

If you like this book, buy it!